<?php

/**
 * 支付相关文件
 * @author yjmp
 */

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
/*用户数据获取*/
$user_info = $GLOBALS['db']->getRow('SELECT * FROM '.$GLOBALS['ecs']->table('users').' WHERE wecha_id = "'.$_SESSION['openids'].'"');
if ($user_info) {
    $smarty->assign('user_info',$user_info);
}

/*位置*/
if ($_SESSION['city_id']) {
   $city = $GLOBALS['db']->getOne('SELECT region_name FROM '.$GLOBALS['ecs']->table('region').' WHERE region_id = "'.$_SESSION['city_id'].'"');
    $smarty->assign('city',$city);
    $smarty->assign('city_id',$_SESSION['city_id']);
}

if ($_SESSION['district']) {
    $smarty->assign('district',$_SESSION['district']);
}

$act = empty($_REQUEST['act'])? '' : $_REQUEST['act'];

if ($act == 'pay') {
	/*商家ID*/
    $shop_id = empty($_REQUEST['shop'])? 0 : $_REQUEST['shop'];
    $smarty->assign('shop_id',$shop_id);
    $shop_info = $GLOBALS['db']->getRow('SELECT * FROM '.$GLOBALS['ecs']->table('goods').' WHERE goods_id = "'.$shop_id.'"');
    $smarty->assign('shop_info',$shop_info);
    $smarty->display('pay_shop.dwt');
}

/*支付动作*/
if ($act == 'action_pay') 
{
	//数据获取
	$shop_id = $_POST['shop_id'];
	$user_id = $_POST['user_id'];
	$waiter = $_POST['waiter'];
	$money = $_POST['money'];
	$add_time = time();
	$add_id = 0;
	//数据验证
	if ($waiter != '') 
	{
		$is_exist = $GLOBALS['db']->getRow('SELECT * FROM '.$GLOBALS['ecs']->table('users').' WHERE user_id = "'.$waiter.'" AND is_waiter = 1 AND from_shop = "'.$shop_id.'"');
		if (!$is_exist) {
			send_err_msg('员工号有误');
		}
	}
	//数据写入
	$GLOBALS['db']->query(' INSERT INTO '.$GLOBALS['ecs']->table('sales_log').' ( user_id, shop_id, waiter_id, money_paid, add_time ) VALUES( "'.$user_id.'", "'.$shop_id.'", "'.$waiter.'", "'.$money.'", "'.$add_time.'" ) ');

	$sales_log_id = $db->insert_id(); 

	//粉丝关系,查询用户是不是商家的粉丝，如果不是，添加粉丝关系
	$is_fans = $GLOBALS['db']->getOne('SELECT COUNT(*) FROM '.$GLOBALS['ecs']->table('shop_fans').' WHERE user_id = "'.$user_id.'" AND shop_id = "'.$shop_id.'"');

	if (!$is_fans) {
		$GLOBALS['db']->query(' INSERT INTO '.$GLOBALS['ecs']->table('shop_fans').' ( user_id, shop_id ) VALUES( "'.$user_id.'", "'.$shop_id.'" )');
	}

	//首次购物判断,关联用户与商家 
	if ($user_info['first_subscrib'] == 0) {
		$GLOBALS['db']->query(' UPDATE '.$GLOBALS['ecs']->table('users').' SET first_subscrib = "'.$shop_id.'" WHERE user_id = "'.$user_info['user_id'].'"');
	}

	//分销逻辑:推荐者用户，服务员，商家，推荐者商家，区域代理
	//推荐人
	$parent_id = get_yj_parent_id($user_id);
	$parent_return_rate = get_return_rate('用户-下线佣金率');

	//如果用户存在推荐人，且存推荐返佣比例不为0，给推荐人发放佣金
	if ($parent_id && $parent_return_rate) {
		$amount = $money * $parent_return_rate / 100;
		$desc = '下线消费返佣';
		send_commission($sales_log_id,$amount,$parent_id,$desc);
	}
	//服务员
	$waiter_return_rate = get_return_rate('店员佣金率');
	if ($waiter_return_rate && $waiter) {
		$amount = $money * $waiter_return_rate / 100;
		$desc = '店铺会员消费反佣';
		send_commission($sales_log_id,$amount,$waiter,$desc);
	}
	//商家
	$shop_return_rate = get_return_rate('商家-消费者佣金率');
	$shop_user_id = $GLOBALS['db']->getOne('SELECT user_id FROM '.$GLOBALS['ecs']->table('goods').' WHERE goods_id = "'.$shop_id.'"');
	if ($shop_return_rate && $shop_user_id) {
		$amount = $money * $shop_return_rate / 100;
		$desc = '会员店内消费返佣';
		
		send_commission($sales_log_id,$amount,$shop_user_id,$desc);
	}
	//推荐者商家(第一次消费的商家)
	$first_sub = get_first_subscrib($user_id);
	$first_sub_rate = get_return_rate('商家-粉丝佣金率');
	if ($first_sub && $first_sub_rate) {
		$amount = $first_sub_rate * $money / 100;
		$desc = '店铺粉丝消费返佣';
		send_commission($sales_log_id,$amount,$first_sub,$desc);
	}
	//区域代理
	$delegate_id = get_delegate($shop_id);
	$dele_rate = get_return_rate('代理-商家佣金率');
	if ($delegate_id && $dele_rate) {
		$amount = $dele_rate * $amount / 100;
		$desc = '区域代理返佣';
		send_commission($sales_log_id,$amount,$delegate_id,$desc);
	}

	//逃单率增长
	escape_rate_change($money,$user_id);

	//商家人气加1
	$GLOBALS['db']->query(' UPDATE '.$GLOBALS['ecs']->table('goods').' SET click_count = click_count + 1 WHERE goods_id = "'.$shop_id.'"');
	
	show_yj_message('操作完成','index.php');
}

//ajax 判断店员是否存在
if ($act == 'chekc_waiter') 
{
	$waiter_id = $_POST['waiter_id'];
	$shop_id = $_POST['shop_id'];
	$res = 0;
	if ($waiter_id && $shop_id) 
	{
		$res = $GLOBALS['db']->getOne('SELECT COUNT(*) FROM '.$GLOBALS['ecs']->table('users').' WHERE user_id = "'.$waiter_id.'" AND is_waiter = 1 AND from_shop = "'.$shop_id.'"');
	}
	echo $res;exit;
}

if ($act == 'test') {
	getIPLoc_QQ();
	exit;
}

/**
 * 获取某个分销关系的比例
 * @param  string $rate_name 分销关系名
 * @return int            分销比例
 */
function get_return_rate($rate_name)
{
	$res = $GLOBALS['db']->getOne('SELECT value FROM '.$GLOBALS['ecs']->table('shop_config').' WHERE code = "'.$rate_name.'"');
	return $res;
}

/**
 * 返佣方法 
 * @param  int $sales_log_id 销售日志ID
 * @param  float $amount       金额
 * @param  int $user_id      用户id
 * @param  string $desc         描述
 * @return null               
 */
function send_commission($sales_log_id,$amount,$user_id,$desc)
{
	$add_time = time();
	//记入日志
	$GLOBALS['db']->query(' INSERT INTO '.$GLOBALS['ecs']->table('commission_log').'( from_id, amount, user_id, description, add_time ) VALUES ( "'.$sales_log_id.'", "'.$amount.'", "'.$user_id.'", "'.$desc.'", "'.$add_time.'" ) ');
	//更新用户数据
	$GLOBALS['db']->query(' UPDATE '.$GLOBALS['ecs']->table('users').' SET user_money = user_money + '.$amount.' WHERE user_id = "'.$user_id.'"');
}
/**
 * 返回用户的推荐用户的id
 * @param  int $user_id 用户ID
 * @return int          推荐人的ID
 */
function get_yj_parent_id($user_id)
{
	$parent_id  = $GLOBALS['db']->getOne('SELECT parent_id FROM '.$GLOBALS['ecs']->table('users').' WHERE user_id = "'.$user_id.'"');
	return $parent_id;
}

/**
 * 获取用户第一次关注的店铺
 * @param  int $user_id 用户id
 * @return int          店铺用户ID
 */
function get_first_subscrib($user_id)
{
	$goods_id = $GLOBALS['db']->getOne('SELECT first_subscrib FROM '.$GLOBALS['ecs']->table('users').' WHERE user_id = "'.$user_id.'"');
	if ($goods_id) {
		return $GLOBALS['db']->getOne('SELECT user_id FROM '.$GLOBALS['ecs']->table('goods').' WHERE goods_id = "'.$goods_id.'"');
	}
	return 0;
}

/**
 * 获取一个店铺所在区域的区域代理
 * @param  int $shop_id 店铺id
 * @return int          区域代理id
 */
function get_delegate($shop_id)
{
	$district = $GLOBALS['db']->getOne('SELECT district FROM '.$GLOBALS['ecs']->table('goods').' WHERE goods_id = "'.$shop_id.'"');
	if ($district) {
		$user_id = $GLOBALS['db']->getOne('SELECT user_id FROM '.$GLOBALS['ecs']->table('users').' WHERE district = "'.$district.'" AND is_delegate = 1');
		if ($user_id) {
			return $user_id;
		}
	}
	return 0;
}

/**
 * 逃单率增长
 * @param  float $money   钱数量
 * @param  int  $user_id 用户ID
 */
function escape_rate_change($money,$user_id)
{
	$pro_rate = $GLOBALS['db']->getOne('SELECT value FROM '.$GLOBALS['ecs']->table('shop_config').' WHERE code = "消费增加逃单率"');
	if ($pro_rate > 0) {
		$true_rate = $money * $pro_rate;
		$org_rate = $GLOBALS['db']->getOne('SELECT escape_rate FROM '.$GLOBALS['ecs']->table('users').' WHERE user_id = "'.$user_id.'"');
		$new_rate = $org_rate + $true_rate;
		if ($new_rate > 100) {
			$new_rate = 100;
			//$GLOBALS['db']->query(' UPDATE '.$GLOBALS['ecs']->table('users').' SET escape_chance = 1 WHERE user_id = "'.$user_id.'"');	
		}
		$GLOBALS['db']->query(' UPDATE '.$GLOBALS['ecs']->table('users').' SET escape_rate = "'.$new_rate.'" WHERE user_id = "'.$user_id.'"');
	}
}
?>
